var ServiceValidation = function () {

    var handleValidation1 = function() {
        // for more info visit the official plugin documentation: 
            // http://docs.jquery.com/Plugins/Validation

            var form1 = $('#form_service');
            var error1 = $('.alert-danger', form1);
            var success1 = $('.alert-success', form1);

            form1.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-block', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                	servicer: {
                        required: true
                    },
                    requirements: {
                        required: true
                    },
                    contactsName: {
                        required: true
                    },
                    customerName: {
                        required: true
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit              
                    success1.hide();
                    error1.show();
                    App.scrollTo(error1, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.form-group').addClass('has-error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change done by hightlight
                    $(element)
                        .closest('.form-group').removeClass('has-error'); // set error class to the control group
                },

                success: function (label) {
                    label
                        .closest('.form-group').removeClass('has-error'); // set success class to the control group
                    
                },

                submitHandler: function (form) {
                	$.ajax({
        	            type:"post",
        	            url:"/crm/admin/services!save.action",
        	            dataType:"json",
        	            data:"customerId="+$("#customerName").val()+"&contactsId="+$("#contactsName").val()+"&requirements="+$("#requirements").html()+
        	            	 "&serviceMode="+$("#serviceMode").val()+"&cannotSatisfyOfRequirements="+$("#cannotSatisfyOfRequirements").html()+
        	            	 "&featuresOfInterest="+$("#featuresOfInterest").html()+"&systemType="+$("#systemType").val()+
        	            	 "&serviceDetail="+$("#serviceDetail").html()+"&nextPlan="+$("#nextPlan").html()
        	            	 +"&creater="+$("#creater").val()+"&createTime="+$("#createTime").val()+"&servicer="+$("#servicer").val()
        	            	 +"&finishTime="+$("#finishTime").val()+"&nextPlanDetail="+$("#nextPlanDetail").val()+"&status="+$("#status").val()
        	            	 +"&serviceDuration="+$("#serviceDuration").val(),
        	            cache:false,
        	            beforeSend:function(XMLHttpRequest){
        	            },
        	            success:function(json){
        	            	result = json.result;
        	            	if(result){
        	            		if(confirm("提示!服务信息添加成功。【确定】键返回列表列表,【取消】键继续添加。")==true){
        	            			window.location.href="/crm/admin/services!list.action";
        	            		}else{
        	            			window.location.href="/crm/admin/services!add.action";
        	            		}
        	            	}else{
        	            		alert("登记失败,重稍后重试!");
        	            	}
        	            },
        	           	complete:function(XMLHttpRequest,textStatus){
        	           	},
        	          	error:function(XMLHttpRequest,textStatus,errorThrown){
        	                   alert("网络错误,重稍后重试!");
        	          	}
                    });
//                    success1.show();
                    error1.hide();
                }
            });


    }

    var handleWysihtml5 = function() {
        if (!jQuery().wysihtml5) {
            
            return;
        }

        if ($('.wysihtml5').size() > 0) {
            $('.wysihtml5').wysihtml5({
                "stylesheets": ["assets/plugins/bootstrap-wysihtml5/wysiwyg-color.css"]
            });
        }
    }

    return {
        //main function to initiate the module
        init: function () {

            handleWysihtml5();
            handleValidation1();

        }

    };

}();